<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Iframe Communication</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
      background-color: #f4f4f4;
    }
    button {
      padding: 10px 15px;
      background-color: #007bff;
      color: white;
      border: none;
      cursor: pointer;
    }
    button:hover {
      background-color: #0056b3;
    }
    #message {
      margin-top: 20px;
      padding: 10px;
      background-color: #e9ecef;
    }
  </style>
</head>
<body>

  <h2>Iframe Page</h2>

  <button onclick="sendMessageToParent()">Send Message to Vue Parent</button>

  <div id="message"></div>

  <img style="width: 20px; height: 20px; margin-top: 20px;" src="../favicon.ico" />

  <script>
    // 接收来自父窗口（Vue）的消息
    window.addEventListener("message", function(event) {
      if (event.origin !== window.location.origin) return; // 安全检查，确保消息来源合法

      const data = event.data;
      if (data && data.action === "update") {
        document.getElementById('message').textContent = "Message from Vue Parent: " + data.message;
      }
    });

    // 向父窗口发送消息
    function sendMessageToParent() {
      window.parent.postMessage({
        action: "update",
        message: "Hello from iframe!"
      }, "*");
    }
  </script>
</body>
</html>
